#include "dvPieChart.h"
#include "nightcharts.h"
#include <QPainter>
#include <QPaintEvent>
#include <QResizeEvent>
#include <QSvgGenerator>

dvPieChart::dvPieChart(QWidget *parent) :
    QWidget(parent), dvDatabaseControllerUsable()
{
    m_legendShow = true;
    m_pChart = new Nightcharts;
    m_pChart->setType(Nightcharts::Dpie);//{Histogramm,Pie,DPie};
    m_pChart->setLegendType(Nightcharts::Vertical);//{Round,Vertical}
    m_pChart->setCords(25,25,width()/1.5,height()/1.5);
    m_title = "";
}

dvPieChart::~dvPieChart()
{
    delete m_pChart;
}

void dvPieChart::clear()
{
    m_title = "";
    m_pChart->clearPieces();
    foreach(int region, m_pieces.keys())
    {
        dvPiePiece *piece = m_pieces.value(region, NULL);
        if (piece)
        {
            m_pieces.remove(region);
            delete piece;
        }
    }
    m_pieces.clear();
}

void dvPieChart::insertPiece(int region, double value, QColor &color, bool attached)
{
    if (m_dbCtrl)
    {
        QString title = m_dbCtrl->getRegionName(QVariant(region)).toString();
        dvPiePiece *piece = new dvPiePiece;
        piece->setName(title);
        piece->setValue(float(value));
        piece->setColor(color);
        piece->setAttached(attached);
        m_pieces.insert(region, piece);
    }
}

void dvPieChart::removePiece(int region)
{
    dvPiePiece *piece = m_pieces.value(region, NULL);
    if (piece)
    {
        m_pieces.remove(region);
        delete piece;
        rebuildPieChart();
    }

}

int dvPieChart::countPieces()
{
    return m_pieces.count();
}

void dvPieChart::rebuildPieChart()
{
    m_pChart->clearPieces();
    foreach(int region, m_pieces.keys())
    {
        dvPiePiece *piece = m_pieces.value(region, NULL);
        if (piece && piece->attached())
        {
            m_pChart->addPiece(piece->name(), piece->color(), piece->value());
        }
    }
    repaint();
}

void dvPieChart::showPiece(int region, bool show)
{
    if (m_pieces.contains(region))
    {
        dvPiePiece *piece = m_pieces.value(region, NULL);
        if (piece)
        {
            piece->setAttached(show);
            rebuildPieChart();
        }
    }
}

void dvPieChart::setHiddenAllPiece(bool hidden)
{
    foreach(int region, m_pieces.keys())
    {
        dvPiePiece *piece = m_pieces.value(region, NULL);
        if (piece)
        {
            piece->setAttached(!hidden);
        }
    }
    rebuildPieChart();
}

void dvPieChart::set3dPie(bool on)
{
    if (on)
    {
        m_pChart->setType(Nightcharts::Dpie);
    }
    else
    {
        m_pChart->setType(Nightcharts::Pie);
    }
    repaint();
}

void dvPieChart::showLegend(bool show)
{
    m_legendShow = show;
    repaint();
}

void dvPieChart::setRoundedLegend(bool on)
{
    if (on)
    {
        m_pChart->setLegendType(Nightcharts::Round);//{Round,Vertical}
    }
    else
    {
        m_pChart->setLegendType(Nightcharts::Vertical);//{Round,Vertical}
    }
    repaint();
}

void dvPieChart::exportImageFile(const QString &fname)
{
    QFile file(fname);
    QFileInfo finfo(file);

    if (finfo.suffix().toUpper() == "BMP" ||
        finfo.suffix().toUpper() == "JPEG" ||
        finfo.suffix().toUpper() == "JPG" ||
        finfo.suffix().toUpper() == "PNG" ||
        finfo.suffix().toUpper() == "PPM" ||
        finfo.suffix().toUpper() == "TIFF" ||
        finfo.suffix().toUpper() == "XBM" ||
        finfo.suffix().toUpper() == "XPM")
    {
        QImage img(width(),
                   height(),
                   QImage::Format_ARGB32_Premultiplied);

        QPainter p(&img);
        p.setRenderHint(QPainter::Antialiasing);
        if (finfo.suffix().toUpper() != "PNG" &&
            finfo.suffix().toUpper() != "TIFF")
        {
            p.fillRect(0,0, width(),
                            height(), QBrush(QColor(Qt::white)));
        }

        render(&p);
        p.end();
        img.save(fname);
    }
    else if (finfo.suffix().toUpper() == "PDF")
    {
        QPrinter pdfPrinter;
        pdfPrinter.setOutputFormat(QPrinter::PdfFormat);
        pdfPrinter.setPaperSize(QSize(width(),height()), QPrinter::Point);
        pdfPrinter.setFullPage(true);
        pdfPrinter.setOutputFileName(fname);

        QPainter pdfPainter;
        pdfPainter.begin( &pdfPrinter);
        render( &pdfPainter );
        pdfPainter.end();
    }
    else if (finfo.suffix().toUpper() == "SVG")
    {
        QSvgGenerator svgGen;
        svgGen.setFileName(fname);
        svgGen.setSize(QSize(width(),height()));
        svgGen.setViewBox(QRect(0, 0, width(), height()));
        svgGen.setTitle(m_title);
        QPainter painter( &svgGen );
        render( &painter );
    }
}

void dvPieChart::paintEvent(QPaintEvent *e)
{
    QPainter painter;

    painter.begin(this);

    painter.drawText(QRectF(0, 0, width(), 25), Qt::AlignTop | Qt::AlignCenter, m_title);
    m_pChart->draw(&painter);

    if (m_legendShow)
    {
        m_pChart->drawLegend(&painter);
    }

    painter.end();

    QWidget::paintEvent(e);
}

void dvPieChart::resizeEvent(QResizeEvent *e)
{
    m_pChart->setCords(25, 50,width()/1.5, height()/1.5);
}
